package uy.edu.fing.pgsegesb.test;

import javax.crypto.SecretKey;

import javax.xml.crypto.dsig.DigestMethod;
import javax.xml.crypto.dsig.SignatureMethod;

import org.w3c.dom.Document;

import uy.edu.fing.pgsegesb.encryption.CipherHelper;
import uy.edu.fing.pgsegesb.integrity.XMLDSignatureHelper;
import uy.edu.fing.pgsegesb.integrity.XMLDSignatureModule;
import uy.edu.fing.pgsegesb.utils.XmlHelper;

public class Main {


	public static void main(String[] args) {

		try {
			
			//probarXMLEncryption();
			
			probarXMLDSig();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	private static void probarXMLDSig() throws Exception {
		
		String tagElement = "";
		
		String msgBodytxt = "<persona id=\"468300000\"> " +  
		"	<nombre>Marvis</nombre>  " +
		"	<apellidos>Rondon Marcelo</apellidos>" +     
		"	<email>marvis@servidor.com</email>" +  
		"	<tarjetaCredito>  " +
		"   	<numero>83838383</numero>  " +
		"    	<fechaExpiracion>01/05</fechaExpiracion>" +  
		"	</tarjetaCredito>" +
		"</persona>";
		
		String xmlMessage = XMLDSignatureHelper.sign(msgBodytxt, tagElement, DigestMethod.SHA1, SignatureMethod.RSA_SHA1);
		
		System.out.println("Mensaje encriptado:\r\n" + xmlMessage);
		XMLDSignatureHelper.validate(xmlMessage);
		
	}


	private static void probarXMLEncryption() throws Exception
	{
		String stdes = "wp4OAt/Ox1d2MQgl6V4xV6vIl7oZKowf";
		SecretKey ktdes = (SecretKey) CipherHelper.keyFromString(stdes, "TripleDES");

		String tagElement = "tarjetaCredito";
		
		String msgBodytxt = "<persona id=\"468300000\"> " +  
		"	<nombre>Marvis</nombre>  " +
		"	<apellidos>Rondon Marcelo</apellidos>" +     
		"	<email>marvis@servidor.com</email>" +  
		"	<tarjetaCredito>  " +
		"   	<numero>83838383</numero>  " +
		"    	<fechaExpiracion>01/05</fechaExpiracion>" +  
		"	</tarjetaCredito>" +
		"</persona>";
		
		String encMsgBodyMan = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><persona id=\"468300000\">" +  	
"<nombre>Marvis</nombre>  	" + 
"<apellidos>Rondon Marcelo</apellidos>	" + 
"<email>marvis@servidor.com</email>	" + 
"<xenc:EncryptedData xmlns:xenc=\"http://www.w3.org/2001/04/xmlenc#\" Type=\"http://www.w3.org/2001/04/xmlenc#Element\"><xenc:EncryptionMethod Algorithm=\"http://www.w3.org/2001/04/xmlenc#aes128-cbc\" xmlns:xenc=\"http://www.w3.org/2001/04/xmlenc#\"/><ds:KeyInfo xmlns:ds=\"http://www.w3.org/2000/09/xmldsig#\">" + 
"<xenc:EncryptedKey xmlns:xenc=\"http://www.w3.org/2001/04/xmlenc#\"><xenc:EncryptionMethod Algorithm=\"http://www.w3.org/2001/04/xmlenc#kw-tripledes\" xmlns:xenc=\"http://www.w3.org/2001/04/xmlenc#\"/><xenc:CipherData xmlns:xenc=\"http://www.w3.org/2001/04/xmlenc#\"><xenc:CipherValue xmlns:xenc=\"http://www.w3.org/2001/04/xmlenc#\">Afr7MDzF+TPalrANoWd9H1UmWteufPmYKvAi4jzD4JY=</xenc:CipherValue></xenc:CipherData></xenc:EncryptedKey></ds:KeyInfo><xenc:CipherData xmlns:xenc=\"http://www.w3.org/2001/04/xmlenc#\"><xenc:CipherValue xmlns:xenc=\"http://www.w3.org/2001/04/xmlenc#\">6aYZty5fCiEwizJecfCDqb18eDsN0XzTBIYKtRIf0JbvlJ0mhOmTSIHCYcTKHVG7YlRgSzlQ0OIjoTB8j/4tAOWzDVvdB/kmqAlA+Utg243AHnhPZzS3AAs1M+qsn9Cpwtl9fQvvGYersBkY6yFR47LgTwgOXSssBMZMg9uZHqTf/LDrsi6qZH0CIBo59EY2</xenc:CipherValue>" + 
"</xenc:CipherData>" + 
"</xenc:EncryptedData>" + 
"</persona>";
		System.out.println(msgBodytxt);
		
		Document msgBody = XmlHelper.stringToXML(msgBodytxt);
		System.out.println(XmlHelper.xmlToString(msgBody));
		
		Document encMsgBody = CipherHelper.encrypt(ktdes, msgBody, tagElement); 
		
		System.out.println(XmlHelper.xmlToString(encMsgBody));
		
		String a = XmlHelper.xmlToString(encMsgBody).replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", "");
		System.out.println("  -----------------------------   ");
		Document encMsgBody2 = XmlHelper.stringToXML(encMsgBodyMan);
		Document msgBody2 = CipherHelper.decrypt(encMsgBody2, ktdes);
		System.out.println(XmlHelper.xmlToString(msgBody2));
	}

}
